package libs

import (
	"crypto/md5"
	"fmt"
	"golang.org/x/crypto/bcrypt"
)

type encrypt struct {
}

func NewEncrypt() *encrypt {
	return &encrypt{}
}

// Md5 md5加密
func (e *encrypt) Md5(string string) string {
	m := fmt.Sprintf("%x", md5.Sum([]byte(string)))
	return m
}

// PasswordHash  生成hash密码
func (e *encrypt) PasswordHash(password string) (string, error) {
	bytes, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost)
	return string(bytes), err
}

// PasswordVerify 校验密码
func (e *encrypt) PasswordVerify(password, hash string) bool {
	err := bcrypt.CompareHashAndPassword([]byte(hash), []byte(password))
	return err == nil
}
